//
//  GHY_DynamicStickersView.m
//  Camera
//
//  Created by aaaaaa on 16/8/22.
//  Copyright © 2016年 葛海一. All rights reserved.
//

#import "GHY_DynamicStickersView.h"
#import "GHY_DynamicStickersCell.h"

@interface GHY_DynamicStickersView ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, retain) UICollectionView *collectionView;
@property (nonatomic, retain) NSMutableArray<NSMutableArray *> *array;




@end

@implementation GHY_DynamicStickersView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self handleData];
        [self addSubview:self.collectionView];
    }
    return self;
}

- (void)handleData{
    
    self.array = [NSMutableArray array];
    [self handleArrWithName:@"ali_" withIndex:78];
    [self handleArrWithName:@"ali_chi" withIndex:26];
    [self handleArrWithName:@"ali_hiaxiu" withIndex:8];
    [self handleArrWithName:@"ali_hongbao" withIndex:9];
    [self handleArrWithName:@"ali_laba" withIndex:8];
    [self handleArrWithName:@"ali_shuai" withIndex:16];
    [self handleArrWithName:@"ali_wuliao" withIndex:4];
}

- (void)handleArrWithName:(NSString *)imageStr withIndex:(NSInteger)index{
    NSMutableArray<UIImage *> *arr = [NSMutableArray array];
    
    for (NSInteger i = index; i > 0; i--) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"%@%ld", imageStr, i]];
        [arr addObject:image];
    }
    [self.array addObject:arr];

}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        flowLayout.itemSize = CGSizeMake(85, 85);
        flowLayout.sectionInset = UIEdgeInsetsMake(0, 7.5, 0, 7.5);
        flowLayout.minimumLineSpacing = 5.0;
        flowLayout.minimumInteritemSpacing = 2.0;
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 120) collectionViewLayout:flowLayout];
        _collectionView.backgroundColor = [UIColor whiteColor];
        _collectionView.showsVerticalScrollIndicator = NO;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[GHY_DynamicStickersCell class] forCellWithReuseIdentifier:@"GHY_DynamicStickersCell"];
    }
    
    return _collectionView;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    GHY_DynamicStickersCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"GHY_DynamicStickersCell" forIndexPath:indexPath];
    
   
    cell.imageArr = self.array[indexPath.row];
    
    return cell;
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.array.count;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

    
    self.selectedCell(self.array[indexPath.row]);
}


@end
